<?php

namespace haibao\weixin\business;

use \haibao\weixin\data\WeixinInfo as WeixinInfoData;
use \haibao\weixin\data\WeixinMenu as WeixinMenuData;
use \haibao\weixin\data\WeixinKeywords as WeixinKeywordsData;
use haibao\cms\business\BusinessException;
use haibao\weixin\data\WeixinArticle as WeixinArticleData;
use haibao\weixin\data\OutSourceEditor as OutSourceEditorData;
use haibao\weixin\data\OutSourcePublicNum as OutSourcePublicNumData;

class WeiXinInfo extends \haibao\weixin\business\Base{
    
    public static $keywordType = array(
        \haibao\weixin\model\data\WeixinKeywords::KEYWORDTYPE_EQUAL =>'等于',
        \haibao\weixin\model\data\WeixinKeywords::KEYWORDTYPE_IN =>'包含',
        \haibao\weixin\model\data\WeixinKeywords::KEYWORDTYPE_HEAD =>'开头位置'
    );
    
    public static $weiXinInfo = array(1=>'海报时尚网',2=>'海报美剧',3=>'FWMA');

    //外包
    public static function addOutEditor($model){
        if($model->Id){
           $outEditorInfo = self::getOneOutEditorInfoById($model->Id);
           $model->CreateTime = $outEditorInfo->CreateTime;
           $model->Status = \haibao\weixin\model\data\OutSourceEditor::STATUS_ENABLE;
           $model->CreateBy = $outEditorInfo->CreateBy;
        }else{
           $model->CreateTime =new \DateTime();
           $model->CreateBy = \haibao\cms\business\Base::getCurrentUser()->Id;
        }
        $outEditorData = new OutSourceEditorData();
        return $outEditorData->saveOutSourceEditor($model);
    }
    
    public static function addOutEditorPubNum($model){
        if($model->Id){
            $outEditorInfo = self::getOneOutEditorPubNumById($model->Id);
            $model->CreateTime = $outEditorInfo->CreateTime;
            $model->Status = \haibao\weixin\model\data\OutSourceEditor::STATUS_ENABLE;
            $model->CreateBy = $outEditorInfo->CreateBy;
        }else{
            $model->CreateTime =new \DateTime();
            $model->CreateBy = \haibao\cms\business\Base::getCurrentUser()->Id;
        }
        $outEditorData = new OutSourcePublicNumData();
        return $outEditorData->saveOutSourceEditor($model);
    }
    
    public static function getOutSourceEditor($filter){
        $outEditorData = new OutSourceEditorData();
        return $outEditorData->getAll($filter);
    }
    
    public static function getOutSourcePubNum($filter){
        $outSourcePublicNumData = new OutSourcePublicNumData();
        return $outSourcePublicNumData->getAll($filter);
    }
    
    public static function getOneOutEditorInfoById($infoId){
        $outEditorData = new OutSourceEditorData();
        return $outEditorData->getOneById($infoId);
    }
    
    public static function getOneOutEditorPubNumById($infoId){
        $utSourcePublicNumData = new OutSourcePublicNumData();
        return $utSourcePublicNumData->getOneById($infoId);
    }
    
    public static function getOneOutPublicNumInfoById($infoId){
        $outSourcePublicNumData = new OutSourcePublicNumData();
        return $outSourcePublicNumData->getOneById($infoId);
    }
    
    public static function delOutSourceEditorById($id, $arrSet){
        $outEditorData = new OutSourceEditorData();
        return $outEditorData->updateById($id, $arrSet);
    }
    
    public static function delOutSourcePubNumById($id, $arrSet){
        $outSourcePublicNumData = new OutSourcePublicNumData();
        return $outSourcePublicNumData->updateById($id, $arrSet);
    }
    
    public static function getAllOutSourceEditorId(){
        $outEditorData = new OutSourceEditorData();
        return $outEditorData->getAllOutSourceEditorId();
    }
    //
    public static function getWeixinName(){
        $weixinInfoData = new WeixinInfoData();
        return $weixinInfoData->getWeixinName();
    }
    
	public static function saveWeixinReadHistory($model){
		$data = new WeixinInfoData();
	
		return $data->add($model);
	}
	
	public static function saveWeixinHistory($historyModel){
		$data = new WeixinInfoData();
		
		return $data->add($historyModel);
	}
	
	public static function getWeixinPlatformTokenCache($accountId){
		$data = new WeiXinInfoData();
		
		return $data->getWeixinPlatformTokenCache($accountId);
	}
	
	public static function delWeixinPlatformTokenCache($accountId){
		$data = new WeiXinInfoData();
		
		$data->delWeixinPlatformTokenCache($accountId);
	}
	
	public static function setWeixinPlatformTokenCache($accountId,$token,$expires = 7100){
		$data = new WeiXinInfoData();
	
		$data->setWeixinPlatformTokenCache($accountId, $token, $expires);
	}
	
	public static function getWeixinJsTicketCache(){
		$data = new WeiXinInfoData();
		return $data->getWeixinJsTicketCache();
	}
	
	public static function setWeixinJsTicketCache($value,$expires = 7200){
		$data = new WeiXinInfoData();
		$data->setWeixinJsTicketCache($value, $expires);
	}
	
	
    public static function getWinxinList(){
        $weixinInfoData = new WeixinInfoData();
        return $weixinInfoData->getWinxinList();
    }
    
    public static function getWeixinInfoById($id){
        $weixinInfoData = new WeixinInfoData();
        return $weixinInfoData->getOneById($id);
    }
    
    public static function updateWinxinInfoStatusById($id,$arrSet){
        $weixinInfoData = new WeixinInfoData();
        $weixinInfoData->updateById($id, $arrSet);
        $data = $weixinInfoData->getOneById($id);
        self::delWeixinCache($data->AppUserKey, 'WeixinInfo');
    }
    
    public static function updateWinxinInfoPublicMsgById($id,$publicMsg){
        $weixinInfoData = new WeixinInfoData();
        $weixinInfoData->updateById($id, $publicMsg);
        $data = $weixinInfoData->getOneById($id);
        self::delWeixinCache($data->AppUserKey, 'WeixinInfo');
    }
    
    public static function saveWeixinInfo($postWinxinData){
        \haibao\cms\business\Base::allowOperation(\haibao\weixin\business\Base::FUNC_CODE_WEIXIN_ADD);
        self::checkWeixinInfo($postWinxinData);
        $weixinInfoData = new WeixinInfoData();
        $weixinInfoData->add($postWinxinData);
        self::delWeixinCache($postWinxinData->AppUserKey, 'WeixinInfo');
    }
    
    public static function editWeixinInfo($postWinxinData){
        \haibao\cms\business\Base::allowOperation(\haibao\weixin\business\Base::FUNC_CODE_WEIXIN_EDIT);
        self::checkWeixinInfo($postWinxinData);
        $weixinInfoData = new WeixinInfoData();
        $weixinInfoData->updateById($postWinxinData->Id, $postWinxinData);
        self::delWeixinCache($postWinxinData->AppUserKey, 'WeixinInfo');
    }
    
    public static function checkWeixinInfo($postWinxinData){
        if(!$postWinxinData){
            throw new BusinessException('error');
        }else{
            if(!$postWinxinData->AccountName){
                throw new BusinessException('微信名不可为空');
            }
        }
    }
    
    public static function getWeixinMenuListById($id,$isMainMenu,$Status = 0){
        $weixinInfoData = new WeixinInfoData();
        return $weixinInfoData->getWeixinMenuListById($id,$isMainMenu,$Status);
    }
    
    public static function getWeixinMenuById($id,$weixinInfoId){
        $weixinMenuData = new WeixinMenuData();
        return $weixinMenuData->getOneById($id);
    }
    
    public static function addWeixinMenu($weixinMenuInfo){
        \haibao\cms\business\Base::allowOperation(\haibao\weixin\business\Base::FUNC_CODE_WEIXIN_MENU_ADD);
        self::checkWeixinMenuInfo($weixinMenuInfo);
        $weixinMenuData = new WeixinMenuData();
        return $weixinMenuData->add($weixinMenuInfo);
    }
    
    public static function updateWeixinMenuById($weixinMenuInfo){
        \haibao\cms\business\Base::allowOperation(\haibao\weixin\business\Base::FUNC_CODE_WEIXIN_MENU_EDIT);
        self::checkWeixinMenuInfo($weixinMenuInfo);
        $weixinMenuData = new WeixinMenuData();
        return $weixinMenuData->updateById($weixinMenuInfo->Id, $weixinMenuInfo);
    }
    
    public static function checkWeixinMenuInfo($weixinMenuInfo){
        $data = array();
        if(!$weixinMenuInfo){
            throw new BusinessException('error');
        }else{
            if($weixinMenuInfo->OperateType == \haibao\weixin\model\data\WeixinMenu::OPERATETYPE_VIEW){
                if($weixinMenuInfo->ToUrlOrKey){
                    if(!preg_match('/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is',$weixinMenuInfo->ToUrlOrKey)){
                        throw new BusinessException('跳转URL格式错误');
                        return false;
                    }
                }
            }
            if (isset($postWinxinData->Id) && $weixinMenuInfo->Id){
                self::isHaveSameWeixinMenuName($weixinMenuInfo->Id,$weixinMenuInfo->Name,$weixinMenuInfo->WeixinInfoId);
            }else{
                self::isHaveSameWeixinMenuName('',$weixinMenuInfo->Name,$weixinMenuInfo->WeixinInfoId);
            }
        }
    }
    
    public static function isHaveSameWeixinMenuName($id,$name,$weixinInfoId){
        $weixinInfoData = new WeixinInfoData();
        $data = $weixinInfoData->isHaveSameWeixinMenuName($id,$name,$weixinInfoId);
        if($data){
            return $data['Id'];
        }else{
            return false;
        }
    }
    
    public static function deleteWeixinMenuById($id,$arrSet){
        \haibao\cms\business\Base::allowOperation(\haibao\weixin\business\Base::FUNC_CODE_WEIXIN_MENU_DELETE);
        $weixinMenuData = new WeixinMenuData();
        return $weixinMenuData->updateById($id, $arrSet);
        
    }
    
    public static function isExistSubMenu($id){
        $weixinInfoData = new WeixinInfoData();
        return $weixinInfoData->isExistSubMenu($id);
    }
    
    public static function getWeixinKeywordsListById($weixinKeywordsFilter){
        $weixinKeywordsData = new WeixinKeywordsData();
        return $weixinKeywordsData->getAll($weixinKeywordsFilter);
    }
    
    public static  function getWeixinKeywordsInfoById($id){
        $weixinKeywordsData = new WeixinKeywordsData();
        return $weixinKeywordsData->getOneById($id);
    }
    
    public static function delkeywordsById($id,$arrSet){
        \haibao\cms\business\Base::allowOperation(\haibao\weixin\business\Base::FUNC_CODE_WEIXIN_KEYWORDS_DELETE);
        $weixinKeywordsData = new WeixinKeywordsData();
        $weixinKeywordsData->updateById($id, $arrSet);
        $data = $weixinKeywordsData->getOneById($id);
        self::delWeixinCache($data->WeixinInfoId, 'Keywords');
    }
    
    public static function addWeixinKeywords($weixinKeywordsInfo){
        \haibao\cms\business\Base::allowOperation(\haibao\weixin\business\Base::FUNC_CODE_WEIXIN_KEYWORDS_ADD);
        $weixinKeywordsData = new WeixinKeywordsData();
        $weixinKeywordsData->add($weixinKeywordsInfo);
        self::delWeixinCache($weixinKeywordsInfo->WeixinInfoId, 'Keywords');
    }
    
    public static function updateWeixinKeywordsById($weixinKeywordsInfo){
       \haibao\cms\business\Base::allowOperation(\haibao\weixin\business\Base::FUNC_CODE_WEIXIN_KEYWORDS_EDIT);
       $weixinKeywordsData = new WeixinKeywordsData();
       $weixinKeywordsData->updateById($weixinKeywordsInfo->Id, $weixinKeywordsInfo);
        self::delWeixinCache($weixinKeywordsInfo->WeixinInfoId, 'Keywords');
    }
    
    /**
     * 通过AppUserKey得微信缓存信息
     * @param string $userKey
     * @return \haibao\weixin\model\data\WeixinInfo
     */
    public static function getInfoByAppUserKey($userKey){
    	$weixinInfoData = new WeixinInfoData();
    	return $weixinInfoData->getInfoByAppUserKey($userKey);
    }
    
    /**
     * 通过微信ID得当前所有有效的关键词
     * @param int $infoId
     */
    public static function getInfoKeywordsById($infoId){
    	$weixinInfoData = new WeixinInfoData();
    	return $weixinInfoData->getInfoKeywordsById($infoId);
    }
    
    /**
     * 添加用户关注信息
     * @param unknown_type $model
     */
    public static function addsaveSubscribeRecord($model){
    	$weixinInfoData = new WeixinInfoData();
    	$weixinInfoData->add($model);
    }
    
    public static  function getWeixinKeywordsAndMenuByWeixinId($id,$type){
        $weixinInfoData = new WeixinInfoData();
        return $weixinInfoData->getWeixinKeywordsAndMenuByWeixinId($id,$type);
    }
    /**
     * 清除缓存 
     * @param 当type为 WeixinInfo，清除微信缓存，$info为$userKey;当type为Keywords时，清除关键字缓存，$info为$infoId(WeixinInfoId)
     * @param 类型 $type
     */
    public static function delWeixinCache($info,$type){
        $cache = new \haibao\weixin\cache\WeixinInfo();
        switch ($type){
            case 'WeixinInfo':
                $cache->delWeixinInfo($info);
                break;
            case 'Keywords':
                $cache->delWeixinKeywords($info);
                break;
        }
    }
    
    public static function clearWeixinOperateHistory($key){
    	$data = new WeixinInfoData();
    	
    	$data->clearWeixinOperateHistory($key);
    }
    
    public static function saveLastOperateHistory($key,$content){
    	$data = new WeixinInfoData();
    
    	$data->saveLastOperateHistory($key,$content);
    }
    
    public static function getWeixinOperateHistory($key){
    	$data = new WeixinInfoData();
    
    	return $data->getWeixinOperateHistory($key);
    }
    
    public static function getAuthUserInfoFromOpenId($openId){
    	$data = new WeixinInfoData();
    	
    	return $data->getAuthUserInfoFromOpenId($openId);
    }
    
    public static function setAuthUserInfo($openId,$userInfo){
        
    	$data = new WeixinInfoData();
    	
    	return $data->setAuthUserInfo($openId,$userInfo);
    }
    
    public static function saveWeixinArticle($weixinArticleData){

        $data = new WeixinArticleData();
         
        return $data->add($weixinArticleData);
    }
    
    public static function getWeixinArticleList($weixinArticleFilter){
        
        $data = new WeixinArticleData();
         
        return $data->getAll($weixinArticleFilter);
    }
    
    public static function getWeixinArticleInfoById($id){
        
        $data = new WeixinArticleData();
         
        return $data->getOneById($id);
    }
    
    public static function updateWeixinArticleById($model){
        self::checkWeixinArticle($model);
        $data = new WeixinArticleData();
        $data->updateById($model->Id, array('WeixinInfoId'=>$model->WeixinInfoId,'Title'=>$model->Title,'Content'=>$model->Content,));
    }
    
    /**
     * 保存生图片
     */
    public static function saveUploadImg($data,$fileName){
    	$basePath = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::IMAGE_UPLOAD_BASE_PATH);
    	$storagePath = $basePath.$fileName;
    	\haibao\cms\business\ImageLibrary::generateImage($storagePath, $data);
    	return $storagePath;
    }
    
    private static function checkWeixinArticle($model){
        if(!$model->Id){
            throw new BusinessException('微信文章id不可为空！');
        }
        if(!$model->WeixinInfoId){
            throw new BusinessException('微信WeixinInfoId不可为空！');
        }
        if(!$model->Title){
            throw new BusinessException('微信Title不可为空！');
        }
        if(!$model->Content){
            throw new BusinessException('微信Content不可为空！');
        }
    }
    
    public static function delWeixinArticleOrImageCollectiveById($id){
        \haibao\cms\business\Base::allowOperation(\haibao\weixin\business\Base::FUNC_CODE_WEIXIN_ARTICLE_DELETE);
        if (!$id){
            throw new BusinessException('ID不能为空');
        }
        $data = new WeixinArticleData();
        $data->removeById($id);
    }
}